import winston from 'winston'

/**
 * 日志记录器
 * @param {string} label 日志标签
 * @param {string} msg 日志消息
 */
const baseLogger = winston.createLogger({
  level: 'info',
  format: winston.format.combine(
    winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    winston.format.printf(({ level, message, label, timestamp }) => {
      return `${timestamp} - 【${level.toUpperCase()}】 - ${label} - ${message}`
    })
  ),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: 'logs/info.log', level: 'info' }),
    new winston.transports.File({ filename: 'logs/error.log', level: 'error' })
  ]
})

const logger = {
  info(label, msg) {
    baseLogger.info({ label: label, message: msg })
  },
  error(label, msg) {
    baseLogger.error({ label: label, message: msg })
  }
}

export default logger
